<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Service\SiteService;
class DomainMiddleware extends BaseMiddleWare
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next) {
        $url = $request->server('HTTP_HOST');
        $start =  mb_strpos($url,"www.")+mb_strlen("www.");
        $end = mb_strpos($url,".com")-$start;
        $domain = mb_substr($url,$start,$end);
        $site = (new SiteService('Admin.Site'))->first(['subdomain'=>$domain]);
        session()->put(['siteId' => $site['id']]);
        session()->put(['siteName'=> $domain]);
        return $next($request);
    }
}
